/**
 * Copyright (C) 2010 Cloudfarming <info@cloudfarming.nl>
 *
 * Licensed under the Eclipse Public License - v 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.eclipse.org/legal/epl-v10.html
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package nl.cloudfarming.client.geoviewer.jxmap.map;

import java.awt.Component;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Set;
import nl.cloudfarming.client.geoviewer.Layer;
import nl.cloudfarming.client.geoviewer.LayerInfo;
import nl.cloudfarming.client.geoviewer.MapController;
import nl.cloudfarming.client.geoviewer.jxmap.map.RootMapPanel.LayerGroup;
import nl.cloudfarming.client.geoviewer.jxmap.render.LayerPainter;
import org.jdesktop.swingx.JXMapViewer;
import org.jdesktop.swingx.JXPanel;
import org.openide.nodes.Node;

/**
 * The WidgetsPanel is used for holding the dynamic map objects, so the user
 * can interact with them, for example showing a tooltip when hover over the
 * object or using a right mouse click context menu (to for example identify
 * the object).
 * 
 * @author Timon Veenstra
 */
public abstract class LayerPanel extends JXPanel implements MapMouseListener, PropertyChangeListener{

    private final MapController controller;
    private final Layer layer;
    private final LayerPainter painter;
    private final LayerInfo info;
    private final JXMapViewer mapViewer;
    private final int id;
    private final LayerGroup group;

    public LayerPanel(final MapController controller, Layer layer, JXMapViewer mapViewer, int id, LayerGroup group) {
        this.id = id;
        this.group = group;
        this.layer = layer;
        this.controller = controller;
        this.painter = new LayerPainter(layer);
        setBackgroundPainter(this.painter);
        this.info = new LayerInfo(false, true, 0);
        this.mapViewer = mapViewer;
        setOpaque(false);

        this.info.addPropertyChangeListener(LayerInfo.PROPERTY_VISIBLE, new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                setVisible((Boolean) evt.getNewValue());
                repaint();
            }
        });


        this.info.addPropertyChangeListener(LayerInfo.PROPERTY_TRANSPARENCY, new PropertyChangeListener() {

            @Override
            public void propertyChange(PropertyChangeEvent evt) {
                setAlpha(info.getTransparency());
                repaint();
            }
        });

    }

    public JXMapViewer getMapViewer() {
        return mapViewer;
    }

    public LayerInfo getInfo() {
        return info;
    }

    public boolean isInterActive() {
        return this.layer.isInterActive();
    }

    public Layer getLayer() {
        return layer;
    }

    public LayerPainter getPainter() {
        return painter;
    }

    public MapController getController() {
        return controller;
    }

    @Override
    public LayerGroup getGroup() {
        return group;
    }

    @Override
    public int getId() {
        return id;
    }

    @Override
    public Component getComponent() {
        return this;
    }    
}
